package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.basedoc.model.OrgBuilding;
import com.eling.elcms.basedoc.model.Room;
import com.eling.elcms.basedoc.model.RoomType;
import com.eling.elcms.basedoc.service.IOrgBuildingManager;
import com.eling.elcms.basedoc.service.IRoomTypeManager;
import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.GroupReserve;
import com.eling.elcms.travel.model.RoomRegister;
import com.eling.elcms.travel.model.view.GroupReserveSaveView;
import com.eling.elcms.travel.model.view.GroupReserveSignUpView;
import com.eling.elcms.travel.model.view.GroupReserveView;
import com.eling.elcms.travel.model.view.RoomView;
import com.eling.elcms.travel.service.IGroupReserveManager;
import com.eling.elcms.travel.service.IRoomRegisterManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class GroupReserveController extends BaseFormController {

	@Autowired
	private IGroupReserveManager groupReserveManager;
	@Autowired
	private IRoomTypeManager roomTypeManager;
	@Autowired
	private IOrgBuildingManager orgBuildingManager;
	@Autowired
	private IRoomRegisterManager roomRegisterManager;
	
	@RequestMapping("/api/groupreserve/save")
	@ResponseBody
	public GroupReserve save(@ModelAttribute @ModelFilling GroupReserve cond) {
		return groupReserveManager.save(cond);
	}
	
	@RequestMapping("/api/groupreserve/rid")
	@ResponseBody
	public Msg rid(@RequestParam(name = "pkRoomRegister")RoomRegister roomRegister,@RequestParam(name = "pkGroupReserve")GroupReserve groupReserve) {
		groupReserveManager.rid(roomRegister, groupReserve);
		return new Msg("删除成功");
	}

	@RequestMapping("/api/groupreserve/saveroom")
	@ResponseBody
	public GroupReserve saveRoom(@ModelAttribute @ModelFilling GroupReserve cond,@RequestParam(value="room") Room room) {
		return groupReserveManager.saveRoom(cond, room);
	}

	@RequestMapping("/api/groupreserve/{pkGroupReserve}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkGroupReserve) {
		groupReserveManager.remove(pkGroupReserve);
		return new Msg("删除成功");
	}

	@RequestMapping("/api/groupreserve/query")
	@ResponseBody
	public List<GroupReserve> query(@ModelAttribute GroupReserve cond) {
		return groupReserveManager.query(cond);
	}

	@RequestMapping("/api/groupreserve/search*")
	@ResponseBody
	public List<GroupReserve> search(@ModelAttribute SearchCondition sc, @ModelAttribute GroupReserve cond) {
		sc.setQueryCondition(cond);
		return groupReserveManager.search(sc);
	}

	/**
	 * 组团预约查询列表
	 * 
	 * @param cond
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/groupreserve/querygroup")
	@ResponseBody
	public List<GroupReserveView> queryGroupReservr(@ModelAttribute GroupReserveView view) {
		return groupReserveManager.queryGroupReservr(view);
	}

	/**
	 * 查询房间状态
	 * @param pkOrgBuilding
	 * @param pkRoomType
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	@RequestMapping("/api/groupreserve/queryroom")
	@ResponseBody
	public List<RoomView> queryRoom(@RequestParam(name = "pkOrgBuilding", required = false) Long pkOrgBuilding,
			@RequestParam(name = "pkRoomType", required = false) Long pkRoomType,
			@RequestParam(name = "startDate", required = false) Date startDate,
			@RequestParam(name = "endDate", required = false) Date endDate) {
		return groupReserveManager.queryRoom(pkOrgBuilding,pkRoomType,startDate,endDate);
	}
	
	/**
	 * 根据组团目的地查询楼宇
	 * @param pkDestination
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/groupreserve/querybuildingbydes")
	@ResponseBody
	public List<OrgBuilding> queryBuildingByDes(@RequestParam(name = "pkDestination", required = false) Long pkDestination) {
		return groupReserveManager.queryBuildingByDes(pkDestination);
	}
	
	/**
	 * 组团预约保存视图
	 * @param grsView
	 * @return
	 * @author zhoulin
	 */
	@RequestMapping("/api/groupreserve/saveall")
	@ResponseBody
	public GroupReserve saveAll(@ModelAttribute GroupReserveSaveView grsView) {
		return groupReserveManager.saveAll(grsView);
	}
	
	@RequestMapping("/api/groupreserve/queryall")
	@ResponseBody
	public GroupReserveSignUpView queryAll(@ModelAttribute GroupReserve cond) {
		return groupReserveManager.queryAll(cond);
	}
	
	@RequestMapping("/api/groupreserve/queryroomtype")
	@ResponseBody
	public List<RoomType> queryRoomType(@RequestParam (value = "orgBuilding") Long pkOrgBuilding) {
		OrgBuilding  orgBuilding = orgBuildingManager.get(pkOrgBuilding);
		Set<OrgBuilding> orgBuildings = new HashSet<>();
		orgBuildings.add(orgBuilding);
		RoomType roomType = new RoomType();
		roomType.setOrgBuildings(orgBuildings);
		return roomTypeManager.query(roomType);
	}

	@RequestMapping("/api/groupreserve/queryplanItem")
	@ResponseBody
	public List<GroupReserve> queryplanItem(@ModelAttribute GroupReserve cond) {
		return groupReserveManager.queryplanItem(cond);
	}
}